package com.woniu.microworkorderserver.businessmag.controller;


import com.alibaba.fastjson.JSONObject;
import com.woniu.microcommonsserver.entity.InspectItemInfoEntity;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microworkorderserver.businessmag.service.InspectItemService;
import com.woniu.microworkorderserver.businessmag.service.InspectTemplateService;
import com.woniu.microworkorderserver.util.JsonToObject;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/inspectItem")
public class InspectItemController {

    @Resource
    private InspectItemService inspectItemServiceImpl;
    @Resource
    private InspectTemplateService inspectTemplateServiceImpl;

    @PostMapping(value = "/save",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg save(@RequestBody JSONObject jsonObject){
        inspectTemplateServiceImpl.saveInspectInfo(JsonToObject.getPollingTemplates(jsonObject),
                JsonToObject.getInspectItems(jsonObject));
        return ResponseMsg.builder().code(200).msg("新增成功").build();
    }

    /**
     * 修改巡检模板数据
     * */
    @PostMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg update(@RequestBody JSONObject jsonObject){
        inspectTemplateServiceImpl.updateInspectInfo(JsonToObject.getPollingTemplates(jsonObject),
                JsonToObject.getInspectItems(jsonObject));
        return ResponseMsg.builder().code(200).msg("修改成功").build();
    }

    @GetMapping(value = "/findByTemplateId",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findByTemplateId(@RequestParam("id") long templateId){
        List<InspectItemInfoEntity> lists = inspectItemServiceImpl.findByTemplateId(templateId);
        return ResponseMsg.builder().code(200).msg("查询成功").data(lists).build();
    }

    @GetMapping(value = "/deleteTemplateId",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg deleteTemplateId(@RequestParam("id") long templateId){
        inspectTemplateServiceImpl.deleteInspectInfo(templateId);
        return ResponseMsg.builder().code(200).msg("删除成功").build();
    }
}
